package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1MagicWeapon;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class ExtraMagicWeaponTable {
  private static final Log _log = LogFactory.getLog(ExtraMagicWeaponTable.class);
  
  private static final Map<Integer, L1MagicWeapon> _magicList = new HashMap<>();
  
  private static ExtraMagicWeaponTable _instance;
  
  public static ExtraMagicWeaponTable getInstance() {
    if (_instance == null)
      _instance = new ExtraMagicWeaponTable(); 
    return _instance;
  }
  
  public final void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM weapon_magic_skill ORDER BY 道具编号");
      rs = pstm.executeQuery();
      while (rs.next()) {
        int item_id = rs.getInt("道具编号");
        String skill_name = rs.getString("显示武器名称");
        int success_random = rs.getInt("强化成功机率");
        int max_use_time = rs.getInt("附加武器时间");
        String success_msg = rs.getString("附加成功讯息");
        String failure_msg = rs.getString("附加失败讯息");
        int probability = rs.getInt("发动机率");
        boolean isLongRange = rs.getBoolean("远距武器发动率");
        int fixDamage = rs.getInt("固定伤害");
        int randomDamage = rs.getInt("随机伤害");
        double doubleDmgValue = rs.getInt("暴击倍率");
        int gfxId = rs.getInt("特效编号");
        boolean gfxIdTarget = rs.getBoolean("目标对象");
        String gfxIdOtherLocStr = rs.getString("额外发动特效座标");
        List<int[]> gfxIdOtherLoc = null;
        if (gfxIdOtherLocStr != null && !gfxIdOtherLocStr.isEmpty()) {
          gfxIdOtherLoc = (List)new ArrayList<>();
          gfxIdOtherLocStr = gfxIdOtherLocStr.replace(" ", "");
          String[] split;
          int length = (split = gfxIdOtherLocStr.split(",")).length;
          int i = 0;
          while (i < length) {
            String value = split[i];
            String[] value2 = value.split("/");
            gfxIdOtherLoc.add(new int[] { Integer.parseInt(value2[0]), Integer.parseInt(value2[1]) });
            i++;
          } 
        } 
        int area = rs.getInt("范围伤害");
        boolean arrowType = rs.getBoolean("飞行效果开关");
        int effectId = rs.getInt("额外技能(skill)");
        int effectTime = rs.getInt("额外技能时间");
        int attr = rs.getInt("伤害附加属性");
        int hpAbsorb = rs.getInt("吸血量");
        int mpAbsorb = rs.getInt("吸魔量");
        int steps = rs.getInt("阶段");
        L1MagicWeapon magicStone = new L1MagicWeapon(item_id, skill_name, success_random, max_use_time, 
            success_msg, failure_msg, probability, isLongRange, fixDamage, randomDamage, doubleDmgValue, 
            gfxId, gfxIdTarget, gfxIdOtherLoc, area, arrowType, effectId, effectTime, attr, hpAbsorb, 
            mpAbsorb, steps);
        int index = item_id * 100 + steps;
        _magicList.put(Integer.valueOf(index), magicStone);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log
      .info("读取->武器魔法DIY资料数量: " + _magicList.size() + "(" + timer.get() + "ms)");
  }
  
  public final L1MagicWeapon get(int id, int steps) {
    int index = id * 100 + steps;
    return _magicList.get(Integer.valueOf(index));
  }
}
